package com.sog.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sog.entity.User;
import com.sog.service.UserServiceI;
import com.sog.service.UserServiceImpl;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//读取用户
		UserServiceI usi=new UserServiceImpl();
		List<User> userlist;
		try {
			int curPage=1;
			int pageSize=3;
			if (null != request.getParameter("curPage")) {
				curPage = Integer.valueOf(request.getParameter("curPage"));
			}
			if (curPage <= 0) {
				curPage = 1;
			}
			//实现前翻页和后翻页  通过jap传递参数act(动作，前翻后翻） cur(当前页面）num(总页数）
			if(null!=request.getParameter("act")) {
				int act=Integer.valueOf(request.getParameter("act"));
				int cur=Integer.valueOf(request.getParameter("cur"));
				int num=Integer.valueOf(request.getParameter("num"));
				if(act==111)
				{
					if(cur<=1)
					{
						curPage=1;
					}
					else {
						curPage=cur-1;
					}
				}else {
					
					if(cur>=num)
					{
						curPage=num;
					}
					else
					{
						curPage=cur+1;
					}
				}
				
			}

			Map<String, Object> map = usi.getPageAll(curPage, pageSize);
			int pageNum=Integer.valueOf(map.get("pageNum").toString());
			
			if(curPage>=pageNum) {
				curPage=pageNum;
			}
			request.setAttribute("map", map);
			request.setAttribute("curPage", curPage);
			//System.out.print(userlist.size());
			request.getRequestDispatcher("/user.jsp").forward(request, response);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
